/*紫微斗数 Chinese Astrology Zi Wei Dou Shu*/
var ziwei = {
	//y:年,m:月,d:日,h:时,g:性别,l:命宫,b:身宫,f:五行局,z:起紫微表,s4:四化星;
	y:null, m:null, d:null, h:null, g:null, l:null, b:null, f:null, s4:null, z:null,
	yS:null, mS:null, dS:null,LunarDay:null, ShengXiao:null,
	y1Pos:null,	y2Pos:null,	hPos:null,	lPos:null,	bPos:null,	zPos:null, Palce:null,
	Place12:null,
	//排紫微命盘
	computeZiWei:function (y_Solar,m_Solar,d_Solar,h_Solar,g_Solar,is_Draw=true){
		//y:年,m:月,d:日,h:时,g:性别,l:命宫,b:身宫,f:五行局,z:起紫微表,s4:四化星;
		this.yS=parseInt(y_Solar);
		this.mS=parseInt(m_Solar);
		this.dS=parseInt(d_Solar);
		if(h_Solar){
			this.dS += Hour2Day(h_Solar);
		}
		simpleLunarVars.lunar.h = simpleLunarVars.lunar2.h = simpleLunarVars.solar.h = null;
		//取得农历时辰，排紫微命盘
		if(simpleLunar(0,this.yS,this.mS,this.dS)===null)
		{
			astroLunar(0,this.yS,this.mS,this.dS);
		}
		if( ! simpleLunarVars.setting.isYueJie)
			this.y=HeavenlyStems[simpleLunarVars.gan.y]+EarthlyBranches[simpleLunarVars.zhi.y];//不依五星要过节
		else
			this.y=HeavenlyStems[simpleLunarVars.gan2.y]+EarthlyBranches[simpleLunarVars.zhi2.y];//要依五星要过节
		this.m=simpleLunarVars.lunar.m;
		this.d=simpleLunarVars.lunar.d;
		if(h_Solar && EarthlyBranches.indexOf(h_Solar) > -1){
			this.h = h_Solar;
		}
		else{ //时间在日子里指定:
			let hf = parseFloat(this.dS);
			let hi = parseInt(this.dS,10);
			if(hf != hi){
				this.h = EarthlyBranches[int2(Math.round((hf - hi)*120000)/10000)];
			}
		}
		this.dS = parseInt(this.dS);
		this.g=g_Solar;
		//年:天干地支
		this.y1Pos=HeavenlyStems.indexOf(this.y.substring(0,1));
		this.y2Pos=EarthlyBranches.indexOf(this.y.substring(1,2));
		//时:地支
		this.hPos=EarthlyBranches.indexOf(this.h);
		//设定紫微斗数
		this.setZiwei(this.d,is_Draw);
		//stepSetStar
		is_Draw && this.stepSetStar(this.y,this.m,this.d,this.h);
		return this.Place12;
	},
	//CenterPart↑↓
	getLunarDay:function(){return simpleLunarVars.lunar.y+"["+HeavenlyStems[simpleLunarVars.gan2.y]+EarthlyBranches[simpleLunarVars.zhi2.y]+"] 年 "+(simpleLunarVars.lunar.l?"闰":"")+simpleLunarVars.lunar.m+" 月 "+simpleLunarVars.lunar.d+" 日 " + this.h + " 时";},
	getSolarDay:function(){return this.yS+ " 年 " + this.mS + " 月 " + this.dS + " 日" + this.h + " 时";},
	getShengXiao:function(){return ShengXiaoGB[simpleLunarVars.zhi2.y];},
	getFiveElement:function(){return this.f;},
	getYinYangGender:function(){return YinYang[this.y1Pos%2]+(this.g=="M"?"男":"女");},
	getJiaZiDay(){return HeavenlyStems[simpleLunarVars.gan2.d] + EarthlyBranches[simpleLunarVars.zhi2.d]},
	//CenterPart↑
	getStarArr:function (STAR,size,pos){
		var starArray = new Array();
		for (i=0;i<size;i++){ starArray[i]=STAR[i][pos]; } return starArray;
	},
	getStarArrByPosArr:function (STAR,size,PosArr){
		var starArray = new Array();
		for (i=0;i<size;i++){ starArray[i]=STAR[i][PosArr[i]]; } return starArray;
	},
	putS04Str:function (starName,STAR){
		return (STAR.indexOf(starName)>=0)?"<b>"+StarM_S04[STAR.indexOf(starName)].substring(1,2)+"</b>":"　";
	},
	getS04Str:function (starName,STAR){
		return (STAR.indexOf(starName)>=0)?StarM_S04[STAR.indexOf(starName)]:"";
	},
	setZiwei:function (d,is_Draw=true){
    	//重排宫位
		if(is_Draw) ziweiUI.clearPalce();
    	//安十二宫，安命宫、身宫
		this.l=EarthlyBranches[((12-this.hPos)+1+this.m*1.0)%12];//l:命宫
		this.b=EarthlyBranches[(12-((22-this.hPos)+1-this.m*1.0)%12)%12];//b:身宫
		this.lPos=EarthlyBranches.indexOf(this.l);
		this.bPos=EarthlyBranches.indexOf(this.b);
		//安五行局
		this.f=FiveElements[FiveEleArr[this.y1Pos%5][((this.lPos-(this.lPos%2==0?0:1))/2)%6]];
		//起紫微表
		this.z=EarthlyBranches[FiveEleTable[FiveElements.indexOf(this.f)][this.d-1]];
		this.zPos=EarthlyBranches.indexOf(this.z);
	},
	stepSetStar:function (y,m,d,h){
		//准备星星
		//0:紫微,1:天机,2:太阳,3:武曲,4:天同,5:廉贞,6:天府,7:太阴,8:贪狼,9:巨门,10:天相,11:天梁,12:七杀,13:破军
		let s14=Star_A14[this.zPos];
		let sZ06=this.getStarArr(Star_Z06,7,this.zPos);
		let sT08=this.getStarArr(Star_T08,8,sZ06[6]);
		//0:文昌 1:文曲 (时) 2:左辅 3:右弼 (月) 4:天魁 5:天钺 6:禄存(年干)
		let sG07=this.getStarArrByPosArr(Star_G07,7,[this.hPos,this.hPos,m-1,m-1,this.y1Pos,this.y1Pos,this.y1Pos]);
		//四化星
		let sS04=this.getStarArr(Star_S04,4,this.y1Pos);
		//六凶星
		let sB06=[Star_B06[0][this.y1Pos],Star_B06[1][this.y1Pos],Star_B06[2][this.y2Pos%4][this.hPos],Star_B06[3][this.y2Pos%4][this.hPos],Star_B06[4][this.hPos],Star_B06[5][this.hPos] ];
		//其他
		let OS05=this.getStarArr(Star_OS5,5,this.y2Pos);
		this.Place12=new Array(12);
		//准备开始组星星
		for (i=0;i<12;i++){
			let StarA,StarB,StarC,Star6,lenStar=[0,0,0,0];
			StarA=[];StarB=[];StarC=[];Star6=[];
			//紫微星系 & 六凶星 
			for (k=0;k<6;k++){
				if (sZ06[k]==i){ StarA[lenStar[0]]=StarM_A14[k]+this.getS04Str(StarM_A14[k],sS04); lenStar[0]+=1; }
				if (sB06[k]==i){ StarB[lenStar[1]]=StarM_B06[k]; lenStar[1]+=1;}
			}
			//天府星系
			for (k=0;k<8;k++){
				if (sT08[k]==i){ StarA[lenStar[0]]=StarM_A14[k+6]+this.getS04Str(StarM_A14[k+6],sS04); lenStar[0]+=1; }
			}
			//六吉星
			for (k=0;k<7;k++){
				if (sG07[k]==i){ Star6[lenStar[3]]=StarM_A07[k]+this.getS04Str(StarM_A07[k],sS04); lenStar[3]+=1; }
			}
			//其他星矅StarO_S0.length
			for (k=0;k<5;k++){
				if (OS05[k]==i){ StarC[lenStar[2]]=StarO_S05[k]; lenStar[2]+=1;}
			}
			//塞入位置
	  		this.Place12[i]={
	  			"MangA": HeavenlyStems[((this.y1Pos%5)*2+(i<2?i+2:i)%10)%10]+"<br/>"+EarthlyBranches[i],
	  			"MangB": Signs12[(12-this.lPos+i)%12],
	  			"MangC": (this.bPos==i?Signs12[12]:""),
	  			"StarA": StarA,"StarB": StarB,"StarC": StarC,"Star6": Star6
	  		};
	  	}
	},
	getDaShian:function (){
		//大限资料准备
		var DaShianYear=DaShian[FiveElements.indexOf(this.f)];
		var MangDirection=this.y1Pos%2; MangDirection+=(this.g=="M")?1:0;
		//小限资料准备
		var SiaoShianYear,SiaoShianStr,SiaoPlace;
		switch(this.y2Pos){
			case 2:  case 6: case 10: SiaoShianYear=4; break;
			case 8:  case 0: case 4:  SiaoShianYear=11; break;
			case 5:  case 9: case 1:  SiaoShianYear=7; break;
			case 11: case 3: case 7:  SiaoShianYear=1; break;
		}
		var DShian=new Array(10);
		var SShian=new Array(10);
		for (i=0;i<12;i++){
			//起大限表
			var DSY=MangDirection==1?DaShianYear+i*10:(DaShianYear-i*10+120)%120;
	  		//起小限表
	  		SiaoShianStr="";
			for (j=0;j<6;j++){
				SiaoShianStr+=","+(j*12+i+1);
			}
			SiaoShianStr=SiaoShianStr.substring(1) + " ... ";
			SiaoPlace=(this.g=="M")?((SiaoShianYear+i)%12+1).toString():((SiaoShianYear-i+12)%12+1).toString();
			DShian[(i+this.lPos)%12+1]=DSY.toString()+" - "+(DSY+9).toString();
			SShian[SiaoPlace]=SiaoShianStr;
		}
		return {"DShian":DShian,"SShian":SShian};
	}
};
